﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Actor : NetworkBehaviour
{
    public uint networkId { get { return netId.Value; } }
    public GameObject fpsCamara;
    public PropertyComponent propertyComponent;
    public GunComponent gunComponent;
    public MoveComponent moveComponent;
    public GameObject hpCanvas;
    
    // Start is called before the first frame update
    void Start()
    {
        Game.GetComp<ActorManager>().Register(networkId, this, isLocalPlayer);
        if (!isLocalPlayer)
        {
            fpsCamara.GetComponent<Camera>().enabled = false;
            if (hpCanvas.GetComponent<LookAtCamera>() == null)
            {
                hpCanvas.AddComponent<LookAtCamera>();
            }
        }
        else
        {
            this.RegisterEvent();
        }
        gunComponent.SetAnimCallBack();
    }
    
    [EventMsg]
    void LeftClick(CBaseEvent ce)
    {
        //开火
        gunComponent.OnFire(-2);
        if (Cursor.lockState == CursorLockMode.None && !Game.UI.CheckGuiRaycastObjects())
        {
            //锁定屏幕
            Cursor.lockState = CursorLockMode.Locked;
        }
    }

    [EventMsg]
    void RightClick(CBaseEvent ce)
    {
        //加血
        gunComponent.OnFire(2);
    }
    
    [EventMsg]
    void HorizontalAndVertical(CBaseEvent ce)
    {
        if (Cursor.lockState == CursorLockMode.Locked)
        {
            float horizontal = (float) ce.Sender;
            float vertical = (float) ce.Sender2;
            //移动
            moveComponent.Move(vertical, horizontal);
        }
    }

    [EventMsg]
    void MouseXY(CBaseEvent ce)
    {
        if (Cursor.lockState == CursorLockMode.Locked)
        {
            float mousex = (float)ce.Sender;
            float mousey = (float)ce.Sender2;
            //旋转镜头
            moveComponent.RotateCamera(mousex, mousey);
        }
    }

    [EventMsg]
    void KeyCodeSpace(CBaseEvent ce)
    {
        //跳跃
        moveComponent.Jump();
    }
    
    [EventMsg]
    void KeyCodeReturn(CBaseEvent ce)
    {
        Cursor.lockState = CursorLockMode.Locked;
    }
    
    [EventMsg]
    void KeyCodeEscape(CBaseEvent ce)
    {
        Cursor.lockState = CursorLockMode.None;
    }

    [EventMsg]
    void KeyCodeR(CBaseEvent ce)
    {
        gunComponent.Reload();
    }
}
